package com.hjm.admin.moule;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;

import com.allen.library.RxHttpUtils;
import com.hjm.admin.moule.utils.Constant;
import com.hjm.admin.moule.utils.SharedPreUtils;
import com.hjm.admin.moule.utils.ThemeUtils;
import com.hjm.admin.moule.view.service.BookDownloadService;
import com.hjm.admin.moule.widget.CircleHeader;
import com.ihsanbal.logging.Level;
import com.ihsanbal.logging.LoggingInterceptor;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.weavey.loading.lib.LoadingLayout;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.internal.platform.Platform;

public class MyApp extends Application {
    private static MyApp app;

    public static Context getAppContext() {
        return app;
    }

    public static Resources getAppResources() {
        return app.getResources();
    }

    public static PackageInfo packageInfo;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    private void init() {
        app = this;
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        startService(new Intent(getAppContext(), BookDownloadService.class));
    initRxHttpUtils();
    initRefresh();
    initLoadingLayout();
}

    private void initLoadingLayout() {
        LoadingLayout.getConfig()
                .setErrorText("出错了 请稍后重试! ")
                .setEmptyText("暂无数据")
                .setNoNetworkText("无网络连接，请检查您的网络···")
                .setErrorImage(R.drawable.ic_error_icon)
                .setEmptyImage(R.drawable.ic_empty_error)
                .setNoNetworkImage(R.drawable.ic_net_error)
                .setAllTipTextColor(R.color.black)
                .setAllTipTextSize(14)
                .setReloadButtonText("点我重试")
                .setReloadButtonTextSize(14)
                .setReloadButtonTextColor(R.color.black)
                .setReloadButtonWidthAndHeight(150, 40);

    }

    private void initRefresh() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreater((context, layout) -> {
            CircleHeader header = new CircleHeader(context);
            layout.setPrimaryColorsId(ThemeUtils.getThemeColorId(), R.color.white);
            return header;
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreater((context, layout) -> new BallPulseFooter(context).setSpinnerStyle(SpinnerStyle.Translate));
    }

    private void initRxHttpUtils() {
        Map map = new HashMap();
        map.put("access-token", SharedPreUtils.getInstance().getString("token", ""));
        map.put("app-type", "Android");
        RxHttpUtils.init(this);
        OkHttpClient.Builder client = new OkHttpClient().newBuilder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(new LoggingInterceptor.Builder()
                        .setLevel(Level.BASIC)
                        .log(Platform.INFO)
                        .request("Request")
                        .response("Response")
                        .build());
        RxHttpUtils.getInstance()
                .config()
                .setBaseUrl(Constant.BASE_URL)
                .setCookie(false)
                .setHeaders(map)
                .setOkClient(client.build())
                .setLog(true);
    }
}
